package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.constant.MessageConstant;
import com.atguigu.dao.MemberDao;
import com.atguigu.dao.OrderDao;
import com.atguigu.dao.OrderSettingDao;
import com.atguigu.entity.Result;
import com.atguigu.pojo.Member;
import com.atguigu.pojo.Order;
import com.atguigu.pojo.OrderSetting;
import com.atguigu.service.OrderService;
import com.atguigu.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderDao orderDao;

    @Autowired
    OrderSettingDao orderSettingDao;

    @Autowired
    MemberDao memberDao;

    @Override
    public Result subscribe(Map map) throws Exception {

        String orderDateStr = (String)map.get("orderDate");
        Date orderDate = DateUtils.parseString2Date(orderDateStr);
        String telephone = (String) map.get("telephone");
        String setmealId = (String) map.get("setmealId");
        String name = (String) map.get("name");
        String sex = (String) map.get("sex");
        Date date = new Date();
        String idCard = (String) map.get("idCard");


        //判断这个日期是否可以预约，可能存在预约满得情况，或者是这天不开放预约

        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderDate);
        if(orderSetting == null){
            return new Result(false, "预约失败，当日没开放预约");
        }
        if(orderSetting.getReservations() >= orderSetting.getNumber()){
            return new Result(false, "预约失败，预约人数已满");
        }

        //判断是否是新用户
        //主键回填
        Member member = memberDao.findUser(telephone);
        if(member == null){

            member = new Member();
            member.setName(name);
            member.setIdCard(idCard);
            member.setRegTime(date);
            member.setPhoneNumber(telephone);
            member.setBirthday(orderDate);
            member.setSex(sex);
            memberDao.add(member);
        }

        //判断这个用户是否是在同一天，同一个套餐多次预约
        Map orderMap = new HashMap();
        orderMap.put("id",member.getId());
        orderMap.put("orderDate",orderDate);
        orderMap.put("setmealId",setmealId);
        int count = orderDao.findUserAndTwos(orderMap);
        if(count > 0){
            return new Result(false, "预约失败，不能重复预约");
        }


        //提交进行预约
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(orderDate);
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId( Integer.parseInt(setmealId));
        try {
            orderDao.subscribe(order);
            return new Result(true,MessageConstant.ORDER_SUCCESS,order);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDER_FAIL);
        }



    }

    @Override
    public Map findById(Integer id) {
        return orderDao.findById(id);
    }
}
